package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.EvaluationRecordEntity;
import com.tfjybj.itoo.exam.model.EvaluationRecordModel;
import com.tfjybj.itoo.exam.provider.service.EvaluationRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * EvaluationRecordController
 * teEvaluationRecord表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-12-18 18:19:48
 */
@Api(tags = {"课程问卷答题记录--EvaluationRecord表接口"})
@RequestMapping(value = "/EvaluationRecord")
@RestController
public class EvaluationRecordController {

    @Resource
    private EvaluationRecordService teEvaluationRecordService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model EvaluationRecordModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody EvaluationRecordModel model) {
        EvaluationRecordEntity teEvaluationRecordEntity = new EvaluationRecordEntity();
        BeanUtils.copyProperties(model, teEvaluationRecordEntity);
        teEvaluationRecordService.save(teEvaluationRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        teEvaluationRecordService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        teEvaluationRecordService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model EvaluationRecordModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "根据id修改teEvaluationRecord")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody EvaluationRecordModel model) {
        EvaluationRecordEntity teEvaluationRecordEntity = new EvaluationRecordEntity();
        BeanUtils.copyProperties(model, teEvaluationRecordEntity);
        teEvaluationRecordService.updateById(teEvaluationRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找TeEvaluationRecord
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        EvaluationRecordEntity teEvaluationRecordEntity = teEvaluationRecordService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teEvaluationRecordEntity);
    }

    /**
     * 分页查询所有TeEvaluationRecord
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "分页查询所有TeEvaluationRecord")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<EvaluationRecordEntity> teEvaluationRecords = teEvaluationRecordService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teEvaluationRecords);
    }
	
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 添加或更新问卷答题记录
     * @param model EvaluationRecordModel
     * @return 添加的结果
     * @author 杨晓慧
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "")
    @PostMapping(value = {"/insertOrUpdateEvaluationRecord"})
    public ItooResult insertOrUpdateEvaluationRecord(@RequestBody EvaluationRecordModel model) {
        EvaluationRecordEntity teEvaluationRecordEntity = new EvaluationRecordEntity();
        BeanUtils.copyProperties(model, teEvaluationRecordEntity);
        return teEvaluationRecordService.insertOrUpdateEvaluationRecord(teEvaluationRecordEntity);
    }

    /**
     * 查询该题干的作答总人数
     * @param paperId  问卷id
     * @param schoolYearId  学期学年id
     * @param courseId 课程id
     * @param evaluationMainId 题干id
     * @author 杨晓慧
     * @since 2.0.0 2018-12-28 17:19:44
     */
    @ApiOperation(value = "查询该题干的作答总人数")
    @GetMapping(value = {"/queryParticipationNumber/{paperId}/{schoolYearId}/{courseId}/{evaluationMainId}"})
    public ItooResult queryParticipationNumber(@ApiParam(value = "问卷id", required = true) @PathVariable String paperId,
                                                 @ApiParam(value = "学年学期", required = true) @PathVariable String schoolYearId,
                                                 @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
                                                 @ApiParam(value = "简答题题干id", required = true) @PathVariable String evaluationMainId) {
        if (StringUtils.isEmpty(paperId)) {
            return ItooResult.build(ItooResult.FAIL, "问卷id为空");
        }
        if (StringUtils.isEmpty(schoolYearId)) {
            return ItooResult.build(ItooResult.FAIL, "学年学期为空");
        }
        if (StringUtils.isEmpty(courseId)) {
            return ItooResult.build(ItooResult.FAIL, "课程id为空");
        }
        if (StringUtils.isEmpty(evaluationMainId)) {
            return ItooResult.build(ItooResult.FAIL, "简答题题干id为空");
        }

        final Integer participationNumber = teEvaluationRecordService.queryParticipationNumber(paperId, schoolYearId, courseId, evaluationMainId);

        return ItooResult.build(ItooResult.SUCCESS, "查询成功",participationNumber);

    }

    /**
     * 实时更新问卷记录到数据库和redis
     * @param model 答题记录实体
     * @author 魏恩君
     * @since 2.0.0 2019年3月20日19:22:42
     */
    @ApiOperation(value = "实时更新问卷记录")
    @PostMapping(value = {"/updateEvaluationRecord"})
    public ItooResult updateEvaluationRecord(@RequestBody EvaluationRecordModel model) {
        if (StringUtils.isEmpty(model.getPaperId())) {
            return ItooResult.build(ItooResult.FAIL, "更新问卷记录，问卷id为空");
        }
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "更新问卷记录，学生id为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "更新问卷记录，课程id为空");
        }
        if (StringUtils.isEmpty(model.getEvaluationMainId())) {
            return ItooResult.build(ItooResult.FAIL, "更新问卷记录，题干id为空");
        }
        if (StringUtils.isEmpty(model.getSchoolYearId())){
            return ItooResult.build(ItooResult.FAIL,"更新问卷记录，学年学期id为空");
        }
        Boolean flag = teEvaluationRecordService.updateEvaluationRecord(model);
        if(flag == true){
            return ItooResult.build(ItooResult.SUCCESS, "实时更新问卷记录成功");
        }
        else{
            return ItooResult.build(ItooResult.FAIL,"实时更新问卷记录失败");
        }
    }

    /**
     * 查某一个问题下的学生作答各选项的比例
     * @param paperId  问卷id
     * @param schoolYearId  学期学年id
     * @param courseId 课程id
     * @param evaluationMainId 题干id
     * @return 各选项的比例
     * @author 杨江涛
     * @since 2.0.0 2019年3月20日19:18:53
     */
    @ApiOperation(value = "查某一个问题下的学生作答各选项")
    @GetMapping(value = {"/queryOptionRatioResult/{paperId}/{schoolYearId}/{courseId}/{evaluationMainId}"})
    public ItooResult queryOptionRatioResult(@ApiParam(value = "问卷id", required = true) @PathVariable String paperId,
                                       @ApiParam(value = "学年学期", required = true) @PathVariable String schoolYearId,
                                       @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
                                       @ApiParam(value = "简答题题干id", required = true) @PathVariable String evaluationMainId) {
        if (StringUtils.isEmpty(paperId)) {
            return ItooResult.build(ItooResult.FAIL, "问卷id为空");
        }
        if (StringUtils.isEmpty(schoolYearId)) {
            return ItooResult.build(ItooResult.FAIL, "学年学期为空");
        }
        if (StringUtils.isEmpty(courseId)) {
            return ItooResult.build(ItooResult.FAIL, "课程id为空");
        }
        if (StringUtils.isEmpty(evaluationMainId)) {
            return ItooResult.build(ItooResult.FAIL, "简答题题干id为空");
        }

        List<EvaluationRecordModel> teEvaluationRecordModels = teEvaluationRecordService.queryOptionRatioResult(paperId, schoolYearId, courseId, evaluationMainId);

        return ItooResult.build(ItooResult.SUCCESS, "查询成功",teEvaluationRecordModels);

    }



}    
